﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    AnnualFee
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 11:11:26
// Update Time         :    2012/6/24 11:11:26
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Repository;
using Gfw.Repositories.Account;
using Gfw.BLL.Basic.ExceptionImpl;
using Gfw.Models.Account;
using Gfw.BLL.Basic.Account;

namespace Gfw.BLL.Basic.Facilities
{
    /// <summary>
    /// 年费，
    /// 注意：这里只包括对帐户转出、转入等明细表操作，不会更改帐户信息表。
    /// </summary>
    public class AnnualFee : IFacility
    {
        ActUserAccountinformation userAccount;
        double amount;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userAccount">用户的帐户信息，缴纳年费以后，将从帐户信息扣除年费金额，但是并不回写到数据库</param>
        /// <param name="amount">需要缴纳的年费金额</param>
        public AnnualFee(ActUserAccountinformation userAccount, double amount)
        {
            if (userAccount.ElectronicMoney < amount)
            {
                throw new GfwException(ConstMessage.EBalanceLessThan);
            }
            this.userAccount = userAccount;
            this.amount = amount; 
        }

        /// <summary>
        /// 注意：作为联合事务，unitOfWork不为空的时候，不更新用户帐户信息表；否则更新帐户信息表
        /// </summary>
        /// <param name="unitOfWork"></param>
        public virtual void Execute(UnitOfWork unitOfWork = null)
        {
            UnitOfWork work = unitOfWork ?? new UnitOfWork();

            
            userAccount.ElectronicMoney = userAccount.ElectronicMoney - amount;

          
            var userEMoneyRepository = new ActUserBargain4emoneyRepository();
            var companyBargainRepository = new ActCompanyBargainRepository();
            //添加用户电子笔交易记录
            userEMoneyRepository.AddOutlay(userAccount.Userid, (int)UserTradeTypeEnum.AnnualFee, amount, userAccount.ElectronicMoney, null, null, work);
            //添加公司
            companyBargainRepository.AddIncome(userAccount.Userid, (int)CompanyTradeTypeEnum.AnnualFee, amount, "", null, null, work);

            //作为单事务，需要提交更改
            if (unitOfWork == null)
            {
                var userAmountRepository = new ActUserAccountinformationRepository();
                userAmountRepository.Update(userAccount, work);
                work.Commit();
            }
        } 
    }
}
